package com.mmlc.meimei.lichengkongjian;

import android.view.View;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.androidnetframe.httputil.BaseRequestFilterLayer;
import com.androidnetframe.responsejavabean.RequestError;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.mmlc.inter.InterActivity;
import com.mmlc.network.filter.ZuiPaiSheDetailRequestFilter;
import com.mmlc.network.responsebean.ZuiPaisheDetailResponseBean;
import com.mmlc.utils.DataUtils;
import com.mmlc.utils.ToastUtils;
import com.mmlc.utils.XUtils;
import com.zhy.adapter.abslistview.CommonAdapter;
import com.zhy.adapter.abslistview.ViewHolder;

import java.util.ArrayList;
import java.util.List;

public class ZuiPaiSheActivity extends InterActivity {


    TextView tv_title;
    PullToRefreshListView pullToRefreshListView;
    List<ZuiPaisheDetailResponseBean.DataBean> datas = new ArrayList<>();
    CommonAdapter<ZuiPaisheDetailResponseBean.DataBean> adapter;

    @Override
    protected int initContentView() {
        return R.layout.activity_zui_pai_she;
    }
    int page = 0;
    String title="";
    @Override
    protected void initView() {
        findViewById(R.id.iv_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });;
        tv_title = (TextView) findViewById(R.id.tv_title);
        title=getIntent().getStringExtra("title");
        tv_title.setText(title);
        pullToRefreshListView = (PullToRefreshListView) findViewById(R.id.pull_refresh_list);
        pullToRefreshListView.setMode(PullToRefreshBase.Mode.BOTH);
        pullToRefreshListView.setAdapter(adapter=new CommonAdapter<ZuiPaisheDetailResponseBean.DataBean>(this, R.layout.content_zuimeipaisedetai,datas) {
            @Override
            protected void convert(ViewHolder viewHolder, final ZuiPaisheDetailResponseBean.DataBean item, int position) {
                GridView g=viewHolder.getView(R.id.grid);
                viewHolder.setText(R.id.content,item.getTitle());
                List<String> list=new ArrayList<String>();
                if(item.getImg_list().size()>9){
                    for(int i=0;i<9;i++){
                        list.add(item.getImg_list().get(i));
                    }
                }else{
                    list.addAll(item.getImg_list());
                }
                g.setAdapter(new CommonAdapter<String>(ZuiPaiSheActivity.this, R.layout.content_zuimeipaishe,list) {
                    @Override
                    protected void convert(ViewHolder viewHolder,final String s, int position) {
                        viewHolder.setVisible(R.id.text,false);
                        XUtils.image((ImageView) viewHolder.getView(R.id.image),s);
                        viewHolder.setOnClickListener(R.id.image, new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                DataUtils.toJump(context,item.getImg_list().indexOf(s), (ArrayList<String>) item.getImg_list());
                            }
                        });
                    }
                });
            }
        });
        pullToRefreshListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) throws InterruptedException {
                datas.clear();
                pullToRefreshListView.setMode(PullToRefreshBase.Mode.BOTH);
                page=1;
                getData();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                page++;
                getData();
            }
        });
        getData();
    }

    private void getData() {
        ZuiPaiSheDetailRequestFilter z=new ZuiPaiSheDetailRequestFilter(this);
        z.requestBean.eday=title;
        z.requestBean.pageSize="20";
        z.requestBean.pageNow=page+"";
        z.sendRequest(new BaseRequestFilterLayer.AccessResultFromFilter<ZuiPaisheDetailResponseBean>() {

            @Override
            public void onSuccess(ZuiPaisheDetailResponseBean result) {
                if (result.getCode() == 0) {
                    datas.addAll(result.getData());
                    adapter.notifyDataSetChanged();
                    pullToRefreshListView.onRefreshComplete();
                    if(result.getData().size()==0){
                        pullToRefreshListView.setMode(PullToRefreshBase.Mode.PULL_FROM_START);
                    }
                }
            }

            @Override
            public void onFailed(View errorLayout, RequestError serverError, int failedType) {
                ToastUtils.showToast(serverError.mess);
            }
        });
    }
}
